/*
 * Copyright (C) 2009 Show SMS open source project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.bydavy.android.showsms.ui;

//~--- non-JDK imports --------------------------------------------------------

import android.app.Activity;

import android.content.Intent;

import android.graphics.Bitmap;

import android.os.Bundle;
import android.os.Handler;

import android.util.Log;

import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.WindowManager;

import android.widget.ImageView;
import android.widget.TextView;

import com.bydavy.android.showsms.R;
import com.bydavy.android.showsms.model.Contact;
import com.bydavy.android.showsms.model.Contact.UpdateContactListener;
import com.bydavy.android.showsms.model.Conversation;
import com.bydavy.android.showsms.model.SimpleMessage;
import com.bydavy.android.showsms.ui.ConversationCenterView.ConversationViewListener;
import com.bydavy.android.showsms.util.AppLog;

//~--- classes ----------------------------------------------------------------

/**
 * Preview activity
 *
 * @version        2.0, 10/03/06
 * @author         Davy L.
 */
public class PreviewActivity extends Activity implements ConversationViewListener, UpdateContactListener {
    //~--- static fields ------------------------------------------------------

    private static final String LOG_TAG = AppLog.LOG_TAG;
    private static final boolean DEBUG  = true;

    //~--- fields -------------------------------------------------------------

    private TextView mNameTV;
    private Handler mHandler = new Handler();
    private ConversationCenterView mConversationCenterView;
    private ConversationBottomView mConversationBottomViewAnimator;
    private Conversation mConversation;
    protected ImageView mPhotoIV;

    //~--- methods ------------------------------------------------------------

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppLog.v("PreviewActivity : onCreate()");

        setContentView(R.layout.preview);

        // Set blur on background
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND, WindowManager.LayoutParams.FLAG_BLUR_BEHIND);

        // Layout View
        mNameTV                         = (TextView) findViewById(R.id.PreviewNameTV);
        mPhotoIV                        = (ImageView) findViewById(R.id.PreviewPhotoSenderImageView);

        mConversationCenterView         = (ConversationCenterView) findViewById(R.id.PreviewMsgViewAnim);
        mConversationBottomViewAnimator = (ConversationBottomView) findViewById(R.id.PreviewBottomViewAnim);

        if (savedInstanceState == null) {
            AppLog.v("PreviewActivity : First creation");

            SimpleMessage simpleMessage = SimpleMessage.constructFromBundle(getIntent().getExtras());

            if (DEBUG) {
                Log.v(LOG_TAG, "SimpleMessage:" + simpleMessage);
            }

            mConversation = Conversation.get(simpleMessage, this, true);
            mConversation.addListenerContact(this);

        } else {
            AppLog.v("PreviewActivity : Restore previous creation");
        }

        populateViews(mConversation);
    }

    /**
     * Construct the mMessage with the intent
     * and give it to the service
     *
     * @param intent
     */
    public void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        AppLog.v("PreviewActivity : onNewIntent()");
    }

    public void onStart() {
        super.onStart();
        AppLog.v("PreviewActivity : onStart()");
    }

    private void populateViews(Conversation conversation) {
        AppLog.v("PreviewActivity : populateViews()");

        mNameTV.setText(conversation.getContact().getDisplayName());
        mConversationCenterView.bind(conversation);
        mConversationCenterView.addListener(this);

        mConversationBottomViewAnimator.bind(conversation);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        AppLog.v("PreviewActivity : onCreateOptionsMenu()");

        MenuInflater inflater = getMenuInflater();

        inflater.inflate(R.menu.preview_menu, menu);

        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        MenuItem menuItemReply    = menu.findItem(R.id.PreviewMenuReply);
        MenuItem menuItemNext     = menu.findItem(R.id.PreviewMenuNextMsg);
        MenuItem menuItemPrevious = menu.findItem(R.id.PreviewMenuPrevMsg);

        menuItemReply.setEnabled(!mConversationBottomViewAnimator.isDisplayedReply());
        menuItemNext.setEnabled(mConversationCenterView.hasNextMessage());
        menuItemPrevious.setEnabled(mConversationCenterView.hasPreviousMessage());

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
        AppLog.v("PreviewActivity : onOptionsItemSelected()");

        switch (item.getItemId()) {
        case R.id.PreviewMenuReply :
            mConversationBottomViewAnimator.showNext();

            break;

        case R.id.PreviewMenuDebug1 :

            break;

        case R.id.PreviewMenuNextMsg :
            mConversationCenterView.nextMsg();

            break;

        case R.id.PreviewMenuPrevMsg :
            mConversationCenterView.previousMsg();

            break;

        default :
            return false;
        }

        return true;
    }

    public void onStop() {
        super.onStop();
        AppLog.v("PreviewActivity : onStop()");
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        AppLog.v("PreviewActivity: onSaveInstanceState()");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        AppLog.v("PreviewActivity: onDestroy()");
    }

    @Override
    public void onConversationViewDoubleTap(MotionEvent e) {
        if (DEBUG) {
            Log.v(LOG_TAG, "PreviewActivity: onConversationViewDoubleTap()");
        }

        mConversationBottomViewAnimator.showNext();
    }

    @Override
    public void onMessageVisibleChanged(int index) {
        mConversationBottomViewAnimator.setVisibleMessage(index);
    }

    @Override
    public void onUpdateContact(Contact c) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                Contact c    = mConversation.getContact();
                Bitmap photo = c.getPhoto(getApplicationContext());

                mPhotoIV.setImageBitmap(photo);
                mNameTV.setText(c.getDisplayName());
            }
        });
    }
}
